package com.warehouse.service;

import com.warehouse.dao.OrderDao;
import com.warehouse.dao.impl.OrderDaoImpl;
import com.warehouse.entity.Cargo;
import com.warehouse.entity.Order;
import com.warehouse.utils.Page;
import com.warehouse.utils.RandomUUID;

import java.util.List;

public class OrderService {
    private OrderDao orderDao = new OrderDaoImpl();

    public Page<Order> queryPage(int start, int pageSize) {
        if (start < 1) {
            start = 1;
        }
        int pageCount = orderDao.count();//总记录数
        int totalPage = pageCount % pageSize == 0 ? (pageCount / pageSize) : (pageCount / pageSize) + 1;//总页数
        if (start >= totalPage) {
            start = totalPage;
        }
        int startPage = (start - 1) * pageSize;//计算当前页数
        List<Order> list = orderDao.allPage(startPage, pageSize);
        return new Page<Order>(start, totalPage, pageSize, list);
    }

    public int add(Order order) {
        order.setId(RandomUUID.generatorUUID());
        order.setPutTime(RandomUUID.NowTimeByS());
        order.setOutTime("");
        return orderDao.add(order);
    }

    public int update(Order order) {
        return orderDao.update(order);
    }


    public int delete(int id) {
        return orderDao.delete(id);
    }

    public Order byId(int id) {
        return orderDao.byId(id);
    }

    public Page<Order> byTypeName(int start, int pageSize, Integer type) {
        if (start < 1) {
            start = 1;
        }
        int pageCount = orderDao.countByAdminNum(type);//总记录数
        int totalPage = pageCount % pageSize == 0 ? (pageCount / pageSize) : (pageCount / pageSize) + 1;//总页数
        if (start >= totalPage) {
            start = totalPage;
        }
        int startPage = (start - 1) * pageSize;//计算当前页数
        List<Order> cargos = orderDao.byAdminNum(startPage, pageSize, type);
        return new Page<Order>(start, totalPage, pageSize, cargos);
    }

    public int updateOut(Order order) {
        order.setOutTime(RandomUUID.NowTimeByS());
        return orderDao.update(order);
    }
}
